<?php

namespace app\admin\controller;

use think\facade\Config;
use think\facade\Request;
use think\facade\Session;
use think\facade\Db;
use think\facade\View;
use app\admin\model\Page as pageModel;
use common\Tree;

class Page extends Common
{


    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        if (request()->isPost()) {
            $Tree = new Tree();
            $cateList = $this->categoryList();
            return ['code' => 0, 'msg' => '获取成功！', 'data' => $cateList, 'rel' => 1];
        }
        return view();
    }

    /**
     * 添加单页
     * @author admin
     * @date 2019-08-08
     */

    public function add()
    {
        try {
            $Tree = new Tree();
            $cateList = $this->categoryList();
            $categorys = $Tree->cate($cateList);
            $this->assign('categorys', $categorys);
            if (Request::isPost()) {
                $data = input('post.');
                $data['create_time'] = time();
                $data = $this->checkfield($this->moduleFields, $data);
                pageModel::create($data);
                return ['code' => 0, 'msg' => '添加成功！'];
            } else {
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 编缉单页
     * @author admin
     * @date 2019-08-08
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['update_time'] = time();
                $where['id'] = $data['id'];
                $data = $this->checkfield($this->moduleFields, $data);
                pageModel::update($data, $where);
                return ['code' => 0, 'msg' => '修改成功！'];
            } else {
                $id = input('id', 0, "intval");
                if (!$id) {
                    return ['code' => 2000, 'msg' => '非法请求！'];
                }
                $info = pageModel::find(['id' => $id]);
                $this->assign('info', $info);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 获取单页所有数据
     * @author admin
     * @date 2019-08-08
     */
    private function categoryList()
    {
        $list = Db::name('page')->order("id", "asc")->select()->toArray();
        return $list;
    }

    /**
     * 列表动态修改内容
     * @author billow.wang
     * @date 2018-04-07
     */
    public function listEdit()
    {
        try {
            $data = input('param.');
            if (pageModel::update($data)) {
                return ['code' => 0, 'msg' => '修改成功！'];
            } else {
                return ['code' => 1, 'msg' => '修改失败！'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 通用删除
     * @author billow.wang
     * @date 2018-04-07
     */
    public function doDelete()
    {
        try {
            $id = input("request.id");
            if (isset($id)) {
                $childCheck = Db::name("page")->where('pid', $id)->count();
                if ($childCheck > 0) {
                    return ['code' => 1, 'msg' => '该栏目下有子栏目！'];
                }
                $is_system =  Db::name("page")->where('id',$id)->where('is_system',1)->find();
                if ($is_system){
                    return ['code' => 1, 'msg' => '系统栏目，不能删除！'];
                }
                $ary_result = Db::name("page")->delete($id);
                if (FALSE !== $ary_result) {
                    return ['code' => 0, 'msg' => '操作成功！'];
                } else {
                    return ['code' => 1, 'msg' => '操作失败！'];
                }
            } else {
                return ['code' => 1, 'msg' => '参数错误！'];
            }
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => '异常！' . $e];
        }
    }


}
